<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Models\Api\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class MessageController extends BaseController
{
    /**
     * 添加
     */
    public function add(Request $request)
    {
        try {
            $params = $request->all();
            Validator::make($params, [
                'userid' => 'required',
                'content' => 'required',
                'title' => 'required'
            ]);
            $params['createtime'] = now();
            if (Message::create($params)) {
                return $this->responseData([], '添加成功', 200);
            } else {
                return $this->responseData([], '添加失败', 200);
            }
        } catch (\Throwable $th) {
            $errorInfo = isset($th->errorInfo) ? $th->errorInfo : "这是一个html $th";
            $this->error_log(__FILE__, __LINE__, json_encode($errorInfo));
            return  $this->responseData($errorInfo, '系统错误', 400);
        }
    }

    /**
     * 获取消息
     */
    public function get($id)
    {
        $data = Message::where('userid', $id)->where('status', 1)->get();
        return $this->responseData($data, '获取成功', 200);
    }
}
